package edu.xidian.onlinedocument.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import edu.xidian.onlinedocument.views.groups.*;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "doc_data")
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class DocumentData implements Serializable {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name = "doc_id")
    @NotNull(groups = DataGroupA.class)
        private Integer docId;

    @Column(name = "doc_name")
    @NotBlank(groups = DataGroupB.class)
    @Size(min = 2, max = 50, message = "文档名称的长度在2到50个字符之间", groups = DataGroupC.class)
        private String docName;

    @Column(name = "user_id")
    @JsonIgnore
        private Integer creatorId;

    @Column(name = "doc_version")
    @NotNull
        private Integer docVersion;

    @Temporal(TemporalType.TIMESTAMP)   //规定Date类型映射为mysql中的DateTime类型
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")    // 用于将Java后端的Date对象转换为指定格式的Json数据传递给前端，也可以指定前端格式
    // @CreationTimestamp  //org.hibernate.annotations.CreationTimestamp，用于在JPA执行insert操作时自动更新该字段值
    @Column(name = "doc_create_time", updatable = false )
        private Date docCreateTime;

    @Temporal(TemporalType.TIMESTAMP)   //规定Date类型映射为mysql中的DateTime类型
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")    // 用于将Java后端的Date对象转换为指定格式的Json数据传递给前端，也可以指定前端格式
    // @UpdateTimestamp    //org.hibernate.annotations.UpdateTimestamp，用于在JPA执行update操作时自动更新该字段值
    @Column(name = "doc_update_time")
        private Date docUpdateTime;

    @Column(name = "doc_is_delete")
        private boolean docIsDelete;

    // ManyToOne最好也设为FetchType.LAZY，否则会影响性能
    // 但是LAZY会导致json序列化出现不明错误，暂且保留初值
    @ManyToOne(/*fetch = FetchType.LAZY*/)
    @JoinColumn(name="user_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    @JsonIgnoreProperties({"createDocumentData", "editDocumentData"})       // 防止连接后生成的json文件产生死循环，双向连接必须要
        private UserData creatorData;

    @OneToMany
    @JoinColumn(name="doc_id", referencedColumnName = "doc_id", insertable = false, updatable = false)
    @JsonIgnoreProperties({"documentData"})       // 防止连接后生成的json文件产生死循环，双向连接必须要
        private List<DocumentEditor> documentEditor;

    @OneToMany
    @JoinColumn(name = "doc_id", referencedColumnName = "doc_id")
    @JsonIgnoreProperties({"documentData"})       // 防止连接后生成的json文件产生死循环，双向连接必须要
        private List<OperationData> operationData;

    public void init(String docName, Integer creatorId, int docVersion, Date docCreateTime, Date docUpdateTime, boolean docIsDelete) {
        this.docName = docName;
        this.creatorId = creatorId;
        this.docVersion = docVersion;
        this.docCreateTime = docCreateTime;
        this.docUpdateTime = docUpdateTime;
        this.docIsDelete = docIsDelete;
    }

    public boolean getDocIsDelete() {
        return docIsDelete;
    }
}
